$(function() {
	$.getJSON(baseURL + "sys/user/info", function(r) {
		$("#realName").text(r.user.realName);
	});
	$.getJSON(baseURL + "comcollectionrecord/companyName", function(r){
		$("#companyName").text(r.companyName);
	});
})

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		comCollectionRecord: {
			gatheringMethod: ""
		},
	},
	methods: {
		getUser: function(){
			$.getJSON(baseURL + "sys/user/info", function(r){
				vm.comCollectionRecord.user = r.user;
			});
		},
		saveOrUpdate: function (event) {
			var comCrChargingitemList = [];
			var allTr = $("#table tr");
			for(var i = 1; i < allTr.length - 1; i++){
				var tr = allTr.eq(i);
				var comCrChargingitem = {};
				comCrChargingitem['chargingItem'] = tr.find('td').eq(0).text().trim();
				comCrChargingitem['serialNo'] = tr.find('td').eq(0).find('input').val();
				comCrChargingitem['univalent'] = tr.find('td').eq(1).find('input').val();
				comCrChargingitem['acreage'] = tr.find('td').eq(2).find('input').val();
				comCrChargingitem['chargingSd'] = tr.find('td').eq(3).find('input').eq(0).val();
				comCrChargingitem['chargingEd'] = tr.find('td').eq(3).find('input').eq(1).val();
				comCrChargingitem['month'] = tr.find('td').eq(4).find("label").text().trim();
				comCrChargingitem['amountPayable'] = tr.find('td').eq(5).find("label").text().trim();
				comCrChargingitem['payAmount'] = tr.find('td').eq(6).find('input').val();
				comCrChargingitem['remark'] = tr.find('td').eq(7).find('textarea').val().trim();
				comCrChargingitem['sort'] = i;
				comCrChargingitemList.push(comCrChargingitem);
			}
			vm.comCollectionRecord.comCrChargingitemList = comCrChargingitemList;
			var url = vm.comCollectionRecord.serialNo == null ? "comcollectionrecord/save" : "comcollectionrecord/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.comCollectionRecord),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							location.href=baseURL + "comcollectionrecord/mimeograph?billNo="+r.billNo+"&token="+token;
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		back: function(event){
			parent.setSrc("modules/com/comcollectionrecord.html");
		}
	}
});
$('#collectionDays').datepicker().on('changeDate.datepicker.amui', function(event) {
	var date= new Date(event.date);
	var year=date.getFullYear();
	var month=date.getMonth() + 1;
	if(month < 10){
		month = '0' + month;
	}
	var day = date.getDate();
	if(day < 10){
		day = '0' + day;
	}
    $('#collectionDays').val(year + '-' + month + '-' + day);
    vm.comCollectionRecord.collectionDays = year + '-' + month + '-' + day;
});
$(".univalent").keyup(function(){
	var univalent = $(this).val();
	var acreage = $(this).parent().next().find("input").val();
	var month = $(this).parent().next().next().next().text();
	var amountPayable = getAmountPayable(univalent,acreage,month);
	if(amountPayable == -1){
		$(this).parent().next().next().next().next().find("label").text("");
	} else {
		$(this).parent().next().next().next().next().find("label").text(amountPayable);
	}
	amountPayableTotal();
});
$(".acreage").keyup(function(){
	var acreage = $(this).val();
	var univalent = $(this).parent().prev().find("input").val();
	var month = $(this).parent().next().next().text();
	var amountPayable = getAmountPayable(univalent,acreage,month);
	if(amountPayable == -1){
		$(this).parent().next().next().next().find("label").text("");
	} else {
		$(this).parent().next().next().next().find("label").text(amountPayable);
	}
	amountPayableTotal();
});
$(".chargingSd").datepicker().on('changeDate.datepicker.amui', function(event) {
	var date = new Date(event.date);
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	if(month < 10){
		month = '0' + month;
	}
	var day = date.getDate();
	if(day < 10){
		day = '0'+ day;
	}
	var chargingSd = year + '-' + month + '-' + day;
	$(this).val(chargingSd);
	var chargingEd = $(this).siblings("input").val();
	var month = getMonth(chargingSd,chargingEd);
	if(month == -1){
		return;
	}
	var billingCycle = $(this).parent();
	billingCycle.next().find("label").text(month);
	var acreage = billingCycle.prev().find("input").val();
	var univalent = billingCycle.prev().prev().find("input").val();
	var amountPayable = getAmountPayable(univalent,acreage,month);
	if(amountPayable == -1){
		return;
	}
	billingCycle.next().next().find("label").text(amountPayable);
	amountPayableTotal();
});
$(".chargingEd").datepicker().on('changeDate.datepicker.amui', function(event) {
	var date = new Date(event.date);
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	if(month < 10){
		month = '0' + month;
	}
	var day = date.getDate();
	if(day < 10){
		day = '0'+ day;
	}
	var chargingEd = year + '-' + month + '-' + day;
	$(this).val(chargingEd);
	var chargingSd = $(this).siblings("input").val();
	var month = getMonth(chargingSd,chargingEd);
	if(month == -1){
		return;
	}
	var billingCycle = $(this).parent();
	billingCycle.next().find("label").text(month);
	var acreage = billingCycle.prev().find("input").val();
	var univalent = billingCycle.prev().prev().find("input").val();
	var amountPayable = getAmountPayable(univalent,acreage,month);
	if(amountPayable == -1){
		return;
	}
	billingCycle.next().next().find("label").text(amountPayable);
	amountPayableTotal();
});
$('.chargingEd').keyup(function(e){
	 if(e.keyCode==8){
		 $(this).val("");
		 $(this).text("");
		
		var billingCycle = $(this).parent();
		 billingCycle.next().find("label").text("");
		 billingCycle.next().next().find("label").text("");
		amountPayableTotal();
	 }
});
$('.chargingSd').keyup(function(e){
	 if(e.keyCode==8){
		 $(this).val("");
		 $(this).text("");
			var billingCycle = $(this).parent();
			 billingCycle.next().find("label").text("");
			 billingCycle.next().next().find("label").text("");
		 amountPayableTotal();
	 }
});
$(".payAmount").keyup(function(){
	payAmountTotal();
});
function getMonth(chargingSd,chargingEd){
	if(!chargingSd.match(new RegExp('^[0-9]+-[0-9]+-[0-9]+.*$'))){
        return -1;
    }
	if(!chargingEd.match(new RegExp('^[0-9]+-[0-9]+-[0-9]+.*$'))){
	    return -1;
	}
    var startTmp = chargingSd.split("-");
    var endTmp = chargingEd.split("-");
    var dS = new Date(startTmp[0],startTmp[1] - 1,startTmp[2]);
    var dE = new Date(endTmp[0],endTmp[1] - 1,endTmp[2]);
    if(dE.getTime() - dS.getTime() < 0){
    	alert("计费开始时间不能大于计费结束时间！");
    	return -1;
    }
    var distance = (dE.getTime() - dS.getTime())/(1000*60*60*24*30);
    var month = Math.round(distance);
    if(month < 1){
    	month = 1;
    }
    return month + "";
}
function getAmountPayable(univalent,acreage,month){
	if(!univalent.match(new RegExp('^[+-]?\\d+(\\.\\d+)?$'))){
        return -1;
    }
	if(!acreage.match(new RegExp('^[+-]?\\d+(\\.\\d+)?$'))){
		return -1;
    }
	if(!month.match(new RegExp('^[+-]?\\d+(\\.\\d+)?$'))){
		return -1;
    }
    var amountPayable = parseFloat(univalent)*parseFloat(acreage)*parseFloat(month);;
    return amountPayable;
}
function amountPayableTotal(){
	var amountPayableTotal = 0.0;
	var rows = $("#table").find("tr").length;
	for(var i = 1; i < rows - 1; i++ ){
		var amountPayable = $("#table tr:eq(" + i + ") td:eq(5)").text();
		if(amountPayable != undefined && amountPayable != "" && amountPayable != null){
			amountPayableTotal += parseFloat(amountPayable);
		}
	}
	$("#amountPayableTotal").text(amountPayableTotal);
};
function payAmountTotal(){
	var payAmountTotal = 0.0;
	var rows = $("#table").find("tr").length;
	for(var i = 1; i < rows - 1; i++ ){
		var payAmount = $("#table tr:eq(" + i + ") td:eq(6) input").val();
		if(payAmount != undefined && payAmount != "" && payAmount != null && payAmount !="-"){
			payAmountTotal += parseFloat(payAmount);
		}
	}
	$("#payAmountTotal").text(payAmountTotal);
};